package com.turnengine.client.local.unit.command.set;

import static com.javabi.common.dependency.DependencyFactory.getDependency;

import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.errorcode.ErrorCodeException;
import com.javabi.command.executor.ICommandExecutorService;
import com.turnengine.client.local.unit.bean.IStorageGroup;
import com.turnengine.client.local.unit.bean.IStorageItem;
import com.turnengine.client.local.unit.bean.IUnit;
import com.turnengine.client.local.unit.bean.IUnitCount;
import com.turnengine.client.local.unit.command.AddUnit;
import com.turnengine.client.local.unit.command.GetAllUnitsAtPlayer;
import com.turnengine.client.local.unit.command.GetStorageGroups;
import com.turnengine.client.local.unit.command.GetStorageItems;
import com.turnengine.client.local.unit.command.GetUnitById;
import com.turnengine.client.local.unit.command.GetUnitByName;
import com.turnengine.client.local.unit.command.GetUnitScore;
import com.turnengine.client.local.unit.command.GetUnits;
import com.turnengine.client.local.unit.command.GetUnitsAtPlayer;
import com.turnengine.client.local.unit.command.GetUnitsByFactionId;
import com.turnengine.client.local.unit.command.GetUnitsByFactionName;
import com.turnengine.client.local.unit.command.GetUnitsByGroupId;
import com.turnengine.client.local.unit.command.GetUnitsByGroupName;
import com.turnengine.client.local.unit.command.IAddUnit;
import com.turnengine.client.local.unit.command.IGetAllUnitsAtPlayer;
import com.turnengine.client.local.unit.command.IGetStorageGroups;
import com.turnengine.client.local.unit.command.IGetStorageItems;
import com.turnengine.client.local.unit.command.IGetUnitById;
import com.turnengine.client.local.unit.command.IGetUnitByName;
import com.turnengine.client.local.unit.command.IGetUnitScore;
import com.turnengine.client.local.unit.command.IGetUnits;
import com.turnengine.client.local.unit.command.IGetUnitsAtPlayer;
import com.turnengine.client.local.unit.command.IGetUnitsByFactionId;
import com.turnengine.client.local.unit.command.IGetUnitsByFactionName;
import com.turnengine.client.local.unit.command.IGetUnitsByGroupId;
import com.turnengine.client.local.unit.command.IGetUnitsByGroupName;
import com.turnengine.client.local.unit.command.ISetStorageGroup;
import com.turnengine.client.local.unit.command.ISetStorageItem;
import com.turnengine.client.local.unit.command.ISetUnitScore;
import com.turnengine.client.local.unit.command.SetStorageGroup;
import com.turnengine.client.local.unit.command.SetStorageItem;
import com.turnengine.client.local.unit.command.SetUnitScore;
import com.turnengine.client.local.unit.enums.UnitMove;
import com.turnengine.client.local.unit.enums.UnitNode;
import com.turnengine.client.local.unit.enums.UnitType;
import java.util.List;

/**
 * The Local Unit Command Set.
 */
public class LocalUnitCommandSet implements ILocalUnitCommandSet {

	@Override
	public IUnit addUnit(long loginId, int instanceId, String name, UnitNode node, UnitType type, UnitMove move, String groupName, String factionName) {
		IAddUnit command = new AddUnit(loginId, instanceId, name, node, type, move, groupName, factionName);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IUnit> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setStorageGroup(long loginId, int instanceId, IStorageGroup group) {
		ISetStorageGroup command = new SetStorageGroup(loginId, instanceId, group);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setStorageItem(long loginId, int instanceId, IStorageItem item) {
		ISetStorageItem command = new SetStorageItem(loginId, instanceId, item);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IUnit getUnitById(long loginId, int instanceId, int id) {
		IGetUnitById command = new GetUnitById(loginId, instanceId, id);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IUnit> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public IUnit getUnitByName(long loginId, int instanceId, String name) {
		IGetUnitByName command = new GetUnitByName(loginId, instanceId, name);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<IUnit> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IUnit> getUnits(long loginId, int instanceId) {
		IGetUnits command = new GetUnits(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IUnit>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IUnit> getUnitsByFactionId(long loginId, int instanceId, int id) {
		IGetUnitsByFactionId command = new GetUnitsByFactionId(loginId, instanceId, id);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IUnit>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IUnit> getUnitsByFactionName(long loginId, int instanceId, String name) {
		IGetUnitsByFactionName command = new GetUnitsByFactionName(loginId, instanceId, name);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IUnit>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IUnit> getUnitsByGroupId(long loginId, int instanceId, int id) {
		IGetUnitsByGroupId command = new GetUnitsByGroupId(loginId, instanceId, id);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IUnit>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IUnit> getUnitsByGroupName(long loginId, int instanceId, String name) {
		IGetUnitsByGroupName command = new GetUnitsByGroupName(loginId, instanceId, name);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IUnit>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public Boolean setUnitScore(long loginId, int instanceId, String name, String groupName, long score) {
		ISetUnitScore command = new SetUnitScore(loginId, instanceId, name, groupName, score);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Boolean> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public long getUnitScore(long loginId, int instanceId, String name, String groupName) {
		IGetUnitScore command = new GetUnitScore(loginId, instanceId, name, groupName);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<Long> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IStorageGroup> getStorageGroups(long loginId, int instanceId) {
		IGetStorageGroups command = new GetStorageGroups(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IStorageGroup>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IStorageItem> getStorageItems(long loginId, int instanceId) {
		IGetStorageItems command = new GetStorageItems(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IStorageItem>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IUnitCount> getAllUnitsAtPlayer(long loginId, int instanceId) {
		IGetAllUnitsAtPlayer command = new GetAllUnitsAtPlayer(loginId, instanceId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IUnitCount>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}

	@Override
	public List<IUnitCount> getUnitsAtPlayer(long loginId, int instanceId, int groupId) {
		IGetUnitsAtPlayer command = new GetUnitsAtPlayer(loginId, instanceId, groupId);
		ICommandExecutorService service = getDependency(ICommandExecutorService.class);
		IExecutableCommandResponse<List<IUnitCount>> response = service.execute(command);
		if (response.hasErrors()) {
			throw new ErrorCodeException(response);
		}
		return response.getReturnValue();
	}
}
